<?php
/**
 * 实现 Laravel 视图组件。
 *
 * @author    Fairyin <fairyin@126.com>
 * @copyright © 2016 www.imcn.vip
 * @version   v1.0
 */

namespace Lib\Twig;

use Illuminate\Contracts\View\View as ViewContract;

class TwigView implements ViewContract
{
    /*
    * View name to render
    * @var string
    * 
    */
    private $view;

    /*
    * Data to pass to the view
    * @var array
    * 
    */
    private $data;

    /*
    * Twig factory
    * @var RAFIE\Twig\TwigFactory
    * 
    */
    private $factory;


    public function __construct(TwigFactory $factory, $view, $data = [])
    {
        $this->factory = $factory;
        if (!strstr($view, '.twig')) {
            $view .= '.twig';
        }
        $this->view = $view;
        $this->data = $data;
    }

    public function render()
    {
        return $this->factory->render($this->view, $this->data);
    }

    public function name()
    {
        return $this->view;
    }

    public function with($key, $value = null)
    {
        $this->data[$key] = $value;

        return $this;
    }
  
    public function __toString()
    {
        return $this->render();
    }
}
